package com.gdk.ksoft.assembly.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.common.ConstantData;
import com.gdk.ksoft.ui.logins.LoginActivity;
import com.gdk.ksoft.ui.logins.DownloadTableActivity;
import com.gdk.ksoft.assembly.widget.CircleProgressBar;
import com.gdk.ksoft.assembly.utils.system.ApkUtils;
import com.gdk.ksoft.assembly.utils.system.ConfigUtils;
import com.gdk.ksoft.assembly.utils.system.FileUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.io.File;

/**
 * Created by hel on 2014-12-10.
 * 下载更新提示框
 */
public class UpdateVersionDialog extends Dialog {

    @ViewInject(R.id.dialog_update_version_cpb)private CircleProgressBar cpb;
    @ViewInject(R.id.dialog_update_version_txtProgress)private TextView txtProgress;
    @ViewInject(R.id.dialog_update_version_txtMax)private TextView txtMax;

    private String downloadUrl = "";//下载地址

    public UpdateVersionDialog(Context context,String downloadUrl) {
        super(context, R.style.WindowDialogTheme);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View rootView = View.inflate(context,R.layout.dialog_update_version,null);
        ViewUtils.inject(this,rootView);
        setContentView(rootView);
        this.downloadUrl = downloadUrl;
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
    }

    /**
     * 下载软件安装包
     */
    public void downloadApk(){
        new HttpUtils().download(downloadUrl, ConstantData.DOWNLOAD_PATH, new RequestCallBack<File>() {

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                super.onLoading(total, current, isUploading);
                txtMax.setText("总长度：" + FileUtils.getFileSize(total));
                txtProgress.setText("已下载：" + FileUtils.getFileSize(current));
                cpb.setMax(total);//设置最大值
                cpb.setProgress(current);//设置当前进度
            }

            @Override
            public void onSuccess(ResponseInfo<File> fileResponseInfo) {
                if(UpdateVersionDialog.this == null) return;//用户或许已经退出了更新，终止接下来的操作
                if (fileResponseInfo.result != null) {
                    try {
                        UpdateVersionDialog.this.dismiss();
                        ConfigUtils.getInstance(getContext()).getEditor()//升级后需要重新下载数据，所以会清空其数据下载日期的记录
                                .putString(LoginActivity.PASSWORD_FIELD,"")//清空登录密码
                                .putString(DownloadTableActivity.DOWNLOAD_DATE_TIME, "")
                                .commit();
                        ApkUtils.installApk(getContext(), fileResponseInfo.result.getAbsolutePath());
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(getContext(), "执行安装失败!原因：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                UpdateVersionDialog.this.dismiss();
                if(UpdateVersionDialog.this == null) return;
                Toast.makeText(getContext(), "下载更新失败!原因：" + s, Toast.LENGTH_SHORT).show();
            }
        });
    }

}
